#!/bin/bash
# 远程非交互版，将该脚本scp到指定的服务器上，然后在中心机ssh 远程执行该脚本
# 因为远程ssh 无法进行交互

salt_config_file=/etc/salt/minion
salt_script=/etc/init.d/salt-minion
zabbix_config_file=/usr/local/zabbix/etc/zabbix_agentd.conf
zabbix_script=/etc/init.d/zabbix_agentd
# ip_list_file=/data/tx_move.ini
# import_script=/data/script/ImportRes.py


colorPrint(){
    case $1 in
    g)
        echo -e "\033[32m$2\033[0m"
        ;;
    r)
        echo -e "\033[31m$2\033[0m"
        ;;
    b)
        echo -e "\033[34m$2\033[0m"
        ;;
    *)
        echo "usage:`basename $0` 颜色 显示内容"
        ;;
    esac
}

changeSaltConfig(){
    # 停止 salt-minion
    ps aux | grep salt-minion | grep -v grep &> /dev/null
#   [ $? -ne 0 ] && colorPrint 'g' 'salt-minion 正在运行中，需要先停止' && read -p "请问是否停止salt-minion(y/n):" stop_choice
#   if [ -nz $stop_choice -a $choice == 'y' -o $choice == 'Y' ];then
    if [ $? == 0 ];then
        for i in `ps aux | grep salt-minion | grep -v grep | awk '{print $2}'`;do
            kill -9 $i
        done
    #else
    #   colorPrint 'r' '不停止salt，退出程序' && exit 1 
    fi

    # 修改配置文件
    local_ip=`ifconfig | grep -A 1 'eth0' | grep inet | awk '{print $2}' | awk -F: '{print $2}'`
    colorPrint 'g' "当前机器内网为：$local_ip,开始修改salt配置文件"
    sed -ri "s/(game|admin|pay|login)-.*/\1-$local_ip/g" $salt_config_file
    colorPrint 'g' "修改后的配置文件内容为 \n`tail -2 $salt_config_file`"
    colorPrint 'g' '开始启动salt-minion================='
    $salt_script start
    colorPrint 'g' 'salt-minion 启动进程如下：'
    ps aux | grep salt-minion | grep -v grep
}

changZabbixConfig(){
    # 停止 zabbix_agent
    ps aux | grep zabbix_agentd | grep -v grep &> /dev/null
    if [ $? == 0 ];then
        ${zabbix_script} stop
    fi
    #[ $? -ne 0 ] && colorPrint 'g' 'zabbix_agentdb 正在运行中，需要先停止' && read -p "请问是否停止zabbix_agentd(y/n):" stop_choice
    #[ -nz $stop_choice -a $choice == 'y' -o $choice == 'Y' ] && $zabbix_script stop || colorPrint 'r' '不停止zabbix_agentd，退出程序' && exit 1
    # [ -nz $stop_choice -a $choice == 'y' -o $choice == 'Y' ] && \ 
    # for i in `ps aux | grep zabbix_agentd | grep -v grep | awk '{print $2}'`;do kill -9 $i ;done || \
    # colorPrint 'r' '不停止zabbix_agentd，退出程序' && exit 1 

    # 修改配置文件
    local_ip=`ifconfig | grep -A 1 'eth0' | grep inet | awk '{print $2}' | awk -F: '{print $2}'`
    colorPrint 'g' "当前机器内网为：$local_ip,开始修改zabbix_agentd配置文件"
    sed -ri "s/(Hostname=).*/\1$local_ip/g" $zabbix_config_file
    colorPrint 'b' "修改后的配置文件内容为 \n`grep Hostname $zabbix_config_file`"
    colorPrint 'g' '开始启动zabbix_agentd================='
    $zabbix_script start
    colorPrint 'g' 'zabbix_agentd 启动进程如下：'
    ps aux | grep zabbix_agentd | grep -v grep
}

getRemoteInnerIP(){
    ifconfig | grep -A 1 'eth0' | grep inet | awk '{print $2}' | awk -F: '{print $2}'
}

stopMysql(){
    /etc/init.d/mysqld stop
}


case $1 in
'changeSaltConfig')
    changeSaltConfig
    ;;
'changZabbixConfig')
    changZabbixConfig
    ;;
'getRemoteInnerIP')
    getRemoteInnerIP
    ;;
'stopMysql')
    stopMysql
    ;;
*)
    colorPrint 'g' '`usage:basename $0` 函数名称'
    ;;
esac